package com.zxx.pattern.mementoPattern.mementor1;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author zxx on 2021/3/16
 */
public class TestMemento {

    public static void main(String[] args) {
        // 创建备忘录集合类
        UserCaretaker caretaker = UserCaretaker.getInstance();
        // 创建用户表
        List<HashMap<String, String>> table1 = new ArrayList<>();

        // 向表中添加数据
        table1.add(makeMap("Jack", "20", "Beijing"));
        table1.add(makeMap("Rose", "16", "Shanghai"));
        table1.add(makeMap("Tom", "22", "Nanjing"));
        table1.add(makeMap("David", "10", "Qingdao"));

        // 存储为备忘录
        caretaker.saveMemento("data_added", table1);

        System.out.println("----------初始化数据表--------------------");
        selectAllFromTable(table1);

        // 修改表中的数据
        List<HashMap<String, String>> table2 = copyList(table1);
        updateData(table2, "Rose", "88", "America");

        // 存储为备忘录
        caretaker.saveMemento("rose_updated", table2);  //这不是相当于两个集合了吗？
        // 打印表中的数据
        System.out.println("--------------修改表中数据---------------");
        //selectAllFromTable(table2);
        //selectAllFromTable(table1);

        // 删除表中的数据
        List<HashMap<String, String>> table3 = copyList(table2);
        deleteData(table3, "Tom");
        // 存储为备忘录
        caretaker.saveMemento("tom_deleted", table3);
        // 打印表中的数据
        System.out.println("--------------删除表中数据---------------");
        selectAllFromTable(table3);
        selectAllFromTable(table2);


        // 回滚到data_added节点
        List<HashMap<String, String>> t = caretaker.rollbackMemento("data_added");
        // 存储为备忘录
        caretaker.saveMemento("rollback_add", t);
        // 打印表中的数据
        System.out.println("--------------回滚到data_added节点---------------");
        selectAllFromTable(t);

    }

    private static void deleteData(List<HashMap<String, String>> table3, String name) {
        for (HashMap<String, String> map : table3) {
            if (map.get("name").equals(name)) {
                table3.remove(map);
            }
        }
    }

    // 更新一条数据
    private static void updateData(List<HashMap<String, String>> table, String name, String age, String address) {
        for (HashMap<String, String> map : table) {
            if (map.get("name").equals(name)) {
                map.put("age", age);
                map.put("address", address);
            }
        }
    }

    // 根据提供的数据生成一条数据
    private static HashMap<String, String> makeMap(String name, String age, String address) {
        HashMap<String, String> map = new HashMap<>();
        map.put("name", name);
        map.put("age", age);
        map.put("address", address);
        return map;
    }

    // SELECT * FROM tb_user
    private static void selectAllFromTable(List<HashMap<String, String>> list) {
        for (HashMap<String, String> map : list) {
            System.out.println(map.get("name") + "\t\t" + map.get("age") + "\t" + map.get("address"));
        }
        System.out.println();
    }


    // 复制一个List（List不能用“=”赋值，只能复制）
    private static List<HashMap<String, String>> copyList(List<HashMap<String, String>> table) {
        List<HashMap<String, String>> list = new ArrayList<>();
        for (HashMap<String, String> map : table) {
            HashMap<String, String> m = new HashMap<>();
            m.put("name", map.get("name"));
            m.put("age", map.get("age"));
            m.put("address", map.get("address"));
            list.add(m);
        }
        return list;
    }

}
